<template>
  <RPIframe v-bind="$attrs" />
</template>
<script>
export default {
  created() {
    this.eventHandler = this._eventHandler.bind(this)
    window.addEventListener('message', this.eventHandler)
    const { width, height } = this.$attrs
    width && this.$emit('SetPageWidth', width)
    height && this.$emit('SetPageHeight', height)
  },
  beforeDestroy() {
    window.removeEventListener('message', this.eventHandler)
  },
  methods: {
    _eventHandler(event) {
      if (event.origin !== location.origin) {
        return
      }

      let eventType = event.data
      if (eventType === 'CloseModelWindow' || eventType === 'RedirectToHistoryList') {
        this.$emit('Close')
      } else if (eventType === 'CloseModelWindowAndRefresh') {
        this.$emit('Close', true)
      }
    }
  }
}
</script>